package com.cabrida.livestar.star;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.main.MainActivity;
import com.cabrida.livestar.message.LetterDetailsActivity;
import com.cabrida.livestar.model.FansGuardData;
import com.cabrida.livestar.model.FansStarData;
import com.cabrida.livestar.model.FansStarHomeData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.model.MsgLetterData;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.view.CircleImageView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
/**
 * 粉丝端明星主页
 */
public class StarActivity extends BaseActivity {
	public static final String TAG = StarActivity.class.getSimpleName();
	private ImageView iv_back;
	private LinearLayout ll_guard;
	private LinearLayout ll_red_packet;
	private LinearLayout ll_gift;
	private LinearLayout ll_record;
	private LinearLayout ll_advance;
	private LinearLayout ll_playback;
	private LinearLayout ll_dynamic;
	
	private CircleImageView iv_header;
	private ImageView iv_bg;
	private TextView tv_sid;
	private TextView tv_name;
	private TextView tv_des1;
	private TextView tv_des2;
	private TextView tv_des3;
	private TextView tv_book;
	private TextView tv_obtain;
	private TextView tv_advance_num;
	private TextView tv_playback_num;
	private TextView tv_dynamic_num;
	private ImageView iv_live_state;
	private ImageView iv_guard_header_1;
	private ImageView iv_guard_header_2;
	private ImageView iv_guard_header_3;
	private ImageView iv_guard_header_4;
	private TextView tv_guard_header_more;
	
	private TextView tv_leave_msg;
	private TextView tv_attention;
	private TextView tv_msg;
	
	private String suid;
	private FansStarHomeData starhomedata = new FansStarHomeData();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}
	
	/** 主页数据 */
	private void setData() {
		tv_sid.setText(starhomedata.getStarData().getS_udid().toString());
		MyUtils.loadImg(starhomedata.getStarData().getU_avatar(), iv_header);
		MyUtils.loadImg(starhomedata.getStarData().getS_backdrop(), iv_bg);
		tv_name.setText(starhomedata.getStarData().getS_name().toString());
		tv_des1.setText(starhomedata.getStarData().getS_musictype().toString());
		tv_book.setText(starhomedata.getStarData().getS_booked().toString());
		tv_obtain.setText(starhomedata.getStarData().getU_obtain().toString());
		tv_advance_num.setText(starhomedata.getAdvance().toString());
		tv_playback_num.setText(starhomedata.getLive().toString());
		tv_dynamic_num.setText(starhomedata.getCircle().toString());
		if (!TextUtils.isEmpty(starhomedata.getLive().toString())&&!"0".equals(starhomedata.getLive().toString())) {
			iv_live_state.setVisibility(View.VISIBLE);
		}else {
			iv_live_state.setVisibility(View.INVISIBLE);
		}
		if ("10".equals(starhomedata.getStarData().getBooked())) {
			tv_attention.setText("已关注");
			tv_attention.setTextColor(getResources().getColor(R.color.text_333));
		}else {
			tv_attention.setText("+关注");
			tv_attention.setTextColor(getResources().getColor(R.color.green));
		}
		iv_guard_header_1.setVisibility(View.GONE);
		iv_guard_header_2.setVisibility(View.GONE);
		iv_guard_header_3.setVisibility(View.GONE);
		iv_guard_header_4.setVisibility(View.GONE);
		tv_guard_header_more.setVisibility(View.GONE);
		if (null!=starhomedata.getList()&&starhomedata.getList().size()>0) {
			if (starhomedata.getList().size()>=1) {
				iv_guard_header_1.setVisibility(View.VISIBLE);
				MyUtils.loadImg(starhomedata.getList().get(0).getU_avatar(), iv_guard_header_1);
			}
			if (starhomedata.getList().size()>=2) {
				iv_guard_header_2.setVisibility(View.VISIBLE);
				MyUtils.loadImg(starhomedata.getList().get(1).getU_avatar(), iv_guard_header_1);
			}
			if (starhomedata.getList().size()>=3) {
				iv_guard_header_3.setVisibility(View.VISIBLE);
				MyUtils.loadImg(starhomedata.getList().get(2).getU_avatar(), iv_guard_header_1);
			}
			if (starhomedata.getList().size()>=4) {
				iv_guard_header_4.setVisibility(View.VISIBLE);
				MyUtils.loadImg(starhomedata.getList().get(3).getU_avatar(), iv_guard_header_1);
			}
			String guardNum = starhomedata.getGuardNum().toString();
			if (!TextUtils.isEmpty(guardNum)) {
				int gn= Integer.valueOf(guardNum);
				if (starhomedata.getList().size()>=4&&gn>4) {
					tv_guard_header_more.setText(starhomedata.getGuardNum().toString());
					tv_guard_header_more.setVisibility(View.VISIBLE);
				}
			}
		} 
		
	}
	
	/** 获取明星主页数据 */
	private void getStarData() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("suid", suid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_FANS_STAR_HOME_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getAttentionList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONObject star = JsonUtils.getJSONObject(data, "star", new JSONObject());
							FansStarData stardata = new FansStarData();
							stardata.setU_avatar(JsonUtils.getString(star,"u_avatar", ""));
							stardata.setU_obtain(JsonUtils.getString(star,"u_obtain", ""));
							stardata.setS_udid(JsonUtils.getString(star,"s_udid", ""));
							stardata.setS_name(JsonUtils.getString(star,"s_name", ""));
							stardata.setS_backdrop(JsonUtils.getString(star,"s_backdrop", ""));
							stardata.setS_booked(JsonUtils.getString(star,"s_booked", ""));
							stardata.setS_musictype(JsonUtils.getString(star,"s_musictype", ""));
							stardata.setBooked(JsonUtils.getString(star,"booked", ""));
							starhomedata.setStarData(stardata);
							starhomedata.setLive(JsonUtils.getString(data,"live", ""));
							starhomedata.setAdvance(JsonUtils.getString(data,"advance", ""));
							starhomedata.setPlayBack(JsonUtils.getString(data,"playBack", ""));
							starhomedata.setCircle(JsonUtils.getString(data,"circle", ""));
							
							
							JSONObject guard = JsonUtils.getJSONObject(data, "guard", new JSONObject());
							starhomedata.setGuardNum(JsonUtils.getString(guard,"num", ""));
							JSONArray list = JsonUtils.getJSONArray(guard, "list", new JSONArray());
							ArrayList<FansGuardData> infoList = JSONParser.getFansGuardList(list);
							starhomedata.setList(infoList);
							setData();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 点击关注数据 */
	private void getAttentionBtn(final String s_udid) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("follow", s_udid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_VLOG_ATTENTION_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getAttentionBtn", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							String status=JsonUtils.getString(data,"status", "");
							String number=JsonUtils.getString(data,"number", "");
							String num=JsonUtils.getString(data,"num", "");
							showToast(message);
							starhomedata.getStarData().setBooked(status);
							starhomedata.getStarData().setS_booked(number);
							
							if ("10".equals(starhomedata.getStarData().getBooked())) {
								tv_attention.setText("已关注");
								tv_attention.setTextColor(getResources().getColor(R.color.text_333));
							}else {
								tv_attention.setText("+关注");
								tv_attention.setTextColor(getResources().getColor(R.color.green));
							}
							tv_book.setText(starhomedata.getStarData().getS_booked().toString());
							Intent intent = new Intent();
							intent.setAction(MainActivity.ACTION_VLOG_LIST);
							context.sendBroadcast(intent);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_star);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		ll_guard = (LinearLayout) findViewById(R.id.ll_guard);
		ll_red_packet = (LinearLayout) findViewById(R.id.ll_red_packet);
		ll_gift = (LinearLayout) findViewById(R.id.ll_gift);
		ll_record = (LinearLayout) findViewById(R.id.ll_record);
		ll_advance = (LinearLayout) findViewById(R.id.ll_advance);
		ll_playback = (LinearLayout) findViewById(R.id.ll_playback);
		ll_dynamic = (LinearLayout) findViewById(R.id.ll_dynamic);
		iv_header= (CircleImageView) findViewById(R.id.iv_header);
		iv_bg= (ImageView) findViewById(R.id.iv_bg);
		tv_sid=(TextView) findViewById(R.id.tv_sid);
		 tv_name=(TextView) findViewById(R.id.tv_name);
		tv_des1=(TextView) findViewById(R.id.tv_des1);
		 tv_des2=(TextView) findViewById(R.id.tv_des2);
		 tv_des3=(TextView) findViewById(R.id.tv_des3);
		 tv_book=(TextView) findViewById(R.id.tv_book);
		 tv_obtain=(TextView) findViewById(R.id.tv_obtain);
		 tv_advance_num=(TextView) findViewById(R.id.tv_advance_num);
		 tv_playback_num=(TextView) findViewById(R.id.tv_playback_num);
		 tv_dynamic_num=(TextView) findViewById(R.id.tv_dynamic_num);
		 iv_live_state=(ImageView) findViewById(R.id.iv_live_state);
		 iv_guard_header_1=(ImageView) findViewById(R.id.iv_guard_header_1);
		 iv_guard_header_2=(ImageView) findViewById(R.id.iv_guard_header_2);
		 iv_guard_header_3=(ImageView) findViewById(R.id.iv_guard_header_3);
		 iv_guard_header_4=(ImageView) findViewById(R.id.iv_guard_header_4);
		 tv_guard_header_more=(TextView) findViewById(R.id.tv_guard_header_more);
			tv_leave_msg=(TextView) findViewById(R.id.tv_leave_msg);
			tv_attention=(TextView) findViewById(R.id.tv_attention);
			tv_msg=(TextView) findViewById(R.id.tv_msg);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		ll_guard.setOnClickListener(this);
		ll_red_packet.setOnClickListener(this);
		ll_gift.setOnClickListener(this);
		ll_record.setOnClickListener(this);
		ll_advance.setOnClickListener(this);
		ll_playback.setOnClickListener(this);
		ll_dynamic.setOnClickListener(this);
		tv_leave_msg.setOnClickListener(this);
		tv_attention.setOnClickListener(this);
		tv_msg.setOnClickListener(this);
		suid = getIntent().getStringExtra("suid");
		if (!TextUtils.isEmpty(suid)) {
			getStarData();
		}
	}





	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.ll_guard:
			{
			Bundle b = new Bundle();
			b.putString("suid", suid);
			openActivity(StarGuardLvActivity.class,b);
			}
			break;
		case R.id.ll_red_packet:
		{
			Bundle b = new Bundle();
			b.putString("suid", suid);
			openActivity(StarRedPacketLvActivity.class,b);
		}
			break;
		case R.id.ll_gift:
		{
			Bundle b = new Bundle();
			b.putString("suid", suid);
			openActivity(StarGiftLvActivity.class,b);
		}
			break;
		case R.id.ll_record:
		{
			Bundle b = new Bundle();
			b.putString("suid", suid);
			openActivity(StarRecordActivity.class,b);
		}
			break;
		case R.id.ll_advance:
			openActivity(StarAdvanceLvActivity.class);
			break;
		case R.id.ll_playback:
			openActivity(StarPlayBackGvActivity.class);
			break;
		case R.id.ll_dynamic:
			
			break;
		case R.id.tv_leave_msg:
		{
			Bundle b = new Bundle();
			b.putString("suid", suid);
			b.putInt("type", Global.MESSAGE_TYPE_3);
			openActivity(StarLeaveMsgActivity.class,b);
		}
			break;
		case R.id.tv_attention:
			getAttentionBtn(suid);
			break;
		case R.id.tv_msg:
			Bundle b = new Bundle();
				b.putString("type", Global.TYPE_FANS);
				b.putString("suid", suid);
				b.putString("pudid", suid);
			b.putString("parent", Global.U_id);
			b.putString("cid", suid);
			
			openActivity(LetterDetailsActivity.class,b);
			break;

		default:
			break;
		}
	}
}
